﻿CREATE FUNCTION [Mapping].[GetSourceKeyExpression]
(
  @mappingItem int
)
RETURNS nvarchar(500)
AS
BEGIN
  DECLARE @str nvarchar(500)

  SELECT @str = IIF(@str IS NULL, '', @str + ' AND ') + KeyColumn + ' = ' + ISNULL(KeyValue, 'NULL')
    FROM Mapping.SourceKey
    WHERE MappingItem = @mappingItem

  RETURN @str
END
